package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.domain.quality.ProjectSetting;
import com.ruoyi.system.domain.quality.QualityDefectSetting;
import com.ruoyi.system.domain.quality.QualityReceipts;
import com.ruoyi.system.domain.quality.QualityReceiptsDetail;
import com.ruoyi.system.mapper.ProjectSettingMapper;
import com.ruoyi.system.mapper.QualityDefectSettingMapper;
import com.ruoyi.system.mapper.QualityReceiptsDetailMapper;
import com.ruoyi.system.mapper.QualityReceiptsMapper;
import com.ruoyi.system.service.IQualityReceiptsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 质检单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@Service
public class QualityReceiptsServiceImpl extends ServiceImpl<QualityReceiptsMapper, QualityReceipts> implements IQualityReceiptsService {

    @Resource
    private QualityDefectSettingMapper qualityDefectSettingMapper;

    @Resource
    private ProjectSettingMapper projectSettingMapper;

    @Resource
    private QualityReceiptsDetailMapper qualityReceiptsDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAndCheck(QualityReceipts qualityReceipts,LoginUser loginUser ) {
        String code="JYD"+ IdUtil.simpleUUID();
        qualityReceipts.setCreateTime(new Date());
        qualityReceipts.setCreateUser(loginUser.getUser().getUserId());
        qualityReceipts.setCode(code);
        qualityReceipts.setStatus(0L);
        save(qualityReceipts);

        //自动生成质检任务
        //获取物料对应的检验项目
        List<QualityDefectSetting> qualityDefectSettings = qualityDefectSettingMapper.selectList(new QueryWrapper<QualityDefectSetting>().lambda()
                .eq(QualityDefectSetting::getMaterialId, qualityReceipts.getMaterialId()));

        if(IterUtil.isEmpty(qualityDefectSettings)){
            throw new CustomException("该物料未配置检验项目!");
        }

        List<ProjectSetting> projectSettings = projectSettingMapper.selectList(new QueryWrapper<ProjectSetting>().lambda().eq(ProjectSetting::getSettingId, qualityDefectSettings.get(0).getId()));
        if(IterUtil.isEmpty(projectSettings)){
            throw new CustomException("该物料未配置检验项目!");
        }

        projectSettings.stream().forEach(i->{
            QualityReceiptsDetail qualityReceiptsDetail=new QualityReceiptsDetail();
            qualityReceiptsDetail.setReceiptsId(qualityReceipts.getId());
            qualityReceiptsDetail.setCreateTime(new Date());
            qualityReceiptsDetail.setStatus(0L);
            qualityReceiptsDetail.setProjectId(i.getProjectId());
            qualityReceiptsDetailMapper.insert(qualityReceiptsDetail);
        });


        return true;
    }


    @Override
    public List<QualityReceipts> listParam(LambdaQueryWrapper<QualityReceipts> lqw) {
        List<QualityReceipts> list=this.baseMapper.listParam(lqw);
        return list;
    }




}
